//
//  MingxiViewController.m
//  Ten
//
//  Created by anfuer on 2018/8/18.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "MingxiViewController.h"
#import "mingxiCell.h"
#import "SPPageMenu.h"
@interface MingxiViewController ()<SPPageMenuDelegate, UIScrollViewDelegate>
{
    int page;
    NSMutableArray*mary1;
    NSMutableArray*mary2;
    NSMutableArray*mary3;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrW;
@property (weak, nonatomic) IBOutlet UIView *HidenView1;
@property (weak, nonatomic) IBOutlet UIView *HidenView2;
@property (weak, nonatomic) IBOutlet UIView *HidenView3;
@property (weak, nonatomic) IBOutlet UIView *MuneBack;
@property (nonatomic, weak) SPPageMenu *pageMenu;
@property (weak, nonatomic) IBOutlet UIScrollView *scrview;
@property (weak, nonatomic) IBOutlet UITableView *tbv3;
@property (weak, nonatomic) IBOutlet UITableView *tbv2;
@property (weak, nonatomic) IBOutlet UITableView *tbv1;
@end

@implementation MingxiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tbv1.separatorColor=[UIColor clearColor];
    
    _scrW.constant=DEVW*3;
    
    NSArray*tempary=@[@"全部",@"收入",@"支出"];
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, DEVW, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    pageMenu.itemTitleFont=[UIFont systemFontOfSize:14];
    [pageMenu setItems:tempary selectedItemIndex:0];
    pageMenu.permutationWay=SPPageMenuPermutationWayNotScrollEqualWidths;
    // 设置代理
    pageMenu.delegate = self;
    [_MuneBack addSubview:pageMenu];
    _pageMenu = pageMenu;
  
    [tempary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UITableView*tabview=idx==0?_tbv1:idx==1?_tbv2:_tbv3;
        NSMutableArray*mary=idx==0?mary1:idx==1?mary2:mary3;
        tabview.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
          
            [mary removeAllObjects];
            page=0;
            [self loaddata];
        }];
        
        
        tabview.separatorColor=[UIColor clearColor];
        [mary addObject:[NSMutableArray array]];
    }];
  

    // Do any additional setup after loading the view.
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
-(void)loaddata
{
    NSMutableDictionary*dic=[NSMutableDictionary dictionaryWithDictionary:@{@"page":[NSString stringWithFormat:@"%d",page]}];
    if (_pageMenu.selectedItemIndex==1) {
        [dic setObject:@"1" forKey:@"in_add"];
    }else if (_pageMenu.selectedItemIndex==2)
    {
        [dic setObject:@"2" forKey:@"in_add"];
    }else
    {
        [dic removeObjectForKey:@"in_add"];
    }
    [FuncPublic getData:self parameters:dic RequestUrl:salary_records success:^(NSDictionary *acountInfo) {
        NSArray*tempary=acountInfo[@"salary_records"];

        if (_pageMenu.selectedItemIndex==0) {
            mary1=[NSMutableArray arrayWithArray:tempary];
            [_tbv1 reloadData];
        }else if (_pageMenu.selectedItemIndex==1)
        {
             [mary2 addObjectsFromArray:tempary];
             [_tbv2 reloadData];
        }else
        {
             [mary3 addObjectsFromArray:tempary];
             [_tbv3 reloadData];
        }
        
    }];
}
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (labs(toIndex - fromIndex) >= 2) {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:NO];
    } else {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:YES];
    }
    page=0;
    mary1=[NSMutableArray array];
    mary2=[NSMutableArray array];
    mary3=[NSMutableArray array];
    [self loaddata];
}

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView==_scrview) {
        [self.pageMenu moveTrackerFollowScrollView:scrollView];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (_pageMenu.selectedItemIndex) {
        case 0:
            return mary1.count;
            break;
        case 1:
            return mary2.count;
            break;
        case 2:
            return mary3.count;
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSDictionary*dic=_pageMenu.selectedItemIndex==0?mary1[indexPath.row]:_pageMenu.selectedItemIndex==1?mary2[indexPath.row]:mary3[indexPath.row];
    mingxiCell*cell=[mingxiCell CellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell Cellinit:dic];
    return cell;

}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
      NSDictionary*dic=_pageMenu.selectedItemIndex==0?mary1[indexPath.row]:_pageMenu.selectedItemIndex==1?mary2[indexPath.row]:mary3[indexPath.row];
    [MyUtils collviewSelidex:@"明细详情" toVc:self andShopInfo:dic];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
